package com.ljfiction.book.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ljfiction.book.Baner.MyPurseRecycalerbean;
import com.ljfiction.book.R;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Android on 2018/3/7.
 */

public class PayTowLayoutAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater mLayoutInflater;
    private List<MyPurseRecycalerbean.MyPurseRecycalerItembean> datas = new ArrayList<>();
    private int isposition = 0;

    //建立枚举 2个item 类型
    public enum ITEM_TYPE {
        ITEM1,
        ITEM2
    }

    public PayTowLayoutAdapter(Context context, List<MyPurseRecycalerbean.MyPurseRecycalerItembean> datas) {
        this.datas = datas;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE.ITEM1.ordinal()) {
            return new Item1ViewHolder(mLayoutInflater.inflate(R.layout.recycle_first, parent, false));
        } else {
            return new Item2ViewHolder(mLayoutInflater.inflate(R.layout.recycle_tow, parent, false));
        }
    }

    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        AutoUtils.autoSize(holder.itemView);
        if (holder instanceof Item1ViewHolder) {
            ((Item1ViewHolder) holder).lajiaobinum.setText(datas.get(position).getLajibinum());
            ((Item1ViewHolder) holder).renminbinum.setText("￥ " + datas.get(position).getRenminbinum());
            ((Item1ViewHolder) holder).lajiaoimage.setImageResource(R.mipmap.dlj);
            if (position == isposition) {
                ((Item1ViewHolder) holder).itemPurse.setBackgroundResource(R.drawable.shap_purserecycalitemback);
            } else {
                ((Item1ViewHolder) holder).itemPurse.setBackgroundResource(R.drawable.shapepurse_backnoselect);
            }
            if (listener != null) {
                ((Item1ViewHolder) holder).itemPurse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isposition = position;
                        listener.onLayoutClick(v, position);
                    }
                });
            }

        } else if (holder instanceof Item2ViewHolder) {
            ((Item2ViewHolder) holder).lajiaobinum2.setText(datas.get(position).getLajibinum());
            ((Item2ViewHolder) holder).renminbinum2.setText("￥ " + datas.get(position).getRenminbinum());
            ((Item2ViewHolder) holder).zengsong2.setText(datas.get(position).getZengsongtext());
            ((Item2ViewHolder) holder).lajiaoimage2.setImageResource(R.mipmap.dlj);
            if (position == isposition) {
                ((Item2ViewHolder) holder).itemPurse2.setBackgroundResource(R.drawable.shap_purserecycalitemback);
            } else {
                ((Item2ViewHolder) holder).itemPurse2.setBackgroundResource(R.drawable.shapepurse_backnoselect);
            }
            if (listener != null) {
                ((Item2ViewHolder) holder).itemPurse2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        isposition = position;
                        listener.onLayout2Click(v, position);
                    }
                });
            }
        }

    }

    public int getItemViewType(int position) {
        if (position < 2) {
            return ITEM_TYPE.ITEM1.ordinal();
        } else {
            return ITEM_TYPE.ITEM2.ordinal();
        }

        /* return ITEM_TYPE.ITEM2.ordinal();*/
    }

    public int getItemCount() {
        /*return 4;*/
        return 6;
    }

    //头布局 的ViewHolder
    public static class Item1ViewHolder extends RecyclerView.ViewHolder {
        ImageView lajiaoimage;
        TextView lajiaobinum;
        TextView renminbinum;
        LinearLayout itemPurse;

        public Item1ViewHolder(View itemView) {
            super(itemView);
            lajiaoimage = itemView.findViewById(R.id.lajiaoimage);
            lajiaobinum = itemView.findViewById(R.id.lajiaobinum);
            renminbinum = itemView.findViewById(R.id.renminbinum);
            itemPurse = itemView.findViewById(R.id.item_purse);
        }
    }

    //普通item 的ViewHolder
    public static class Item2ViewHolder extends RecyclerView.ViewHolder {
        ImageView lajiaoimage2;
        TextView lajiaobinum2;
        TextView renminbinum2;
        TextView zengsong2;
        LinearLayout itemPurse2;

        public Item2ViewHolder(View itemView) {
            super(itemView);
            lajiaoimage2 = itemView.findViewById(R.id.lajiaoimage2);
            lajiaobinum2 = itemView.findViewById(R.id.lajiaobinum2);
            renminbinum2 = itemView.findViewById(R.id.renminbinum2);
            itemPurse2 = itemView.findViewById(R.id.item_purse2);
            zengsong2 = itemView.findViewById(R.id.zengsong2);
        }
    }

    private OnRecycleViewListener listener;

    public void setOnRecycleViewListener(OnRecycleViewListener listener) {
        this.listener = listener;
    }

    public interface OnRecycleViewListener {
        void onLayoutClick(View view, int position);

        void onLayout2Click(View view, int position);
    }
}
